package com.guigu.serviceorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.IdWorker.IdWorker;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.serviceorder.entity.TbOrder;
import com.guigu.serviceorder.entity.TbOrderConfig;
import com.guigu.serviceorder.entity.TbOrderLog;
import com.guigu.serviceorder.mapper.TbOrderConfigMapper;
import com.guigu.serviceorder.mapper.TbOrderLogMapper;
import com.guigu.serviceorder.mapper.TbOrderMapper;
import com.guigu.serviceorder.service.ITbOrderConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-08
 */
@Service
public class TbOrderConfigServiceImpl extends ServiceImpl<TbOrderConfigMapper, TbOrderConfig> implements ITbOrderConfigService
{

    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private TbOrderConfigMapper tbOrderConfigMapper;

    @Autowired
    private TbOrderLogMapper tbOrderLogMapper;

    @Autowired
    private TbOrderLogServiceImpl tbOrderLogService;


    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {

        QueryWrapper<TbOrderConfig> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbOrderConfig> pageTbOrderConfig = new Page<>(page,size);
        baseMapper.selectPage(pageTbOrderConfig,wrapper);
        if (pageTbOrderConfig.getRecords() == null && pageTbOrderConfig.getTotal() == 0){
            return Result.error();
        }
        return Result.ok().data("rows",pageTbOrderConfig.getRecords()).data("total",pageTbOrderConfig.getTotal());
    }

    //秒杀订单超时
    @Override
    public void seckillOrderTimeOut() {
        //秒杀超时时间
        TbOrderConfig orderConfig =  tbOrderConfigMapper.selectById(1);
        Integer seckillorderTimeout = orderConfig.getSeckillTimeout();//秒杀默认超时时间40分
        DateTime dateTime = DateTime.now().minusMinutes(seckillorderTimeout);
        //设置查询条件
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.le("create_time",dateTime);//创建时间小于超时时间
        wrapper.eq("order_status",0);//未付款的
        wrapper.eq("is_delete","0");//未删除的

        //查询超时订单
        List<TbOrder> orders = tbOrderMapper.selectList(wrapper);
        for ( TbOrder tbOrder : orders){
            //记录订单变动日志
            TbOrderLog tbOrderLog = new TbOrderLog();
            tbOrderLog.setId(String.valueOf(new IdWorker().nextId()));
            tbOrderLog.setOperater("system");
            tbOrderLog.setOperateTime(new Date());
            tbOrderLog.setOrderStatus("4");
            tbOrderLog.setPayStatus(tbOrder.getPayStatus());
            tbOrderLog.setConsignStatus(tbOrder.getConsignStatus());
            tbOrderLog.setRemarks("超时订单,系统自动关闭");
            tbOrderLog.setOrderId(tbOrder.getId());
            tbOrderLogMapper.insert(tbOrderLog);
            //更改订单状态
            tbOrder.setOrderStatus("4");
            tbOrder.setCloseTime(new Date());
            tbOrderMapper.updateById(tbOrder);
        }
    }
    //普通订单
    @Override
    public void orderTimeOut() {
        //查询超时时间
        TbOrderConfig orderConfig =  tbOrderConfigMapper.selectById(1);
        Integer orderTimeout = orderConfig.getOrderTimeout();//超时时间默认60分
        //LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(orderTimeout);//得到超时的时间点
        DateTime dateTime = DateTime.now().minusMinutes(orderTimeout);
        //设置查询条件
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.le("create_time",dateTime);//创建时间小于超时时间
        wrapper.eq("order_status",0);//未付款的
        wrapper.eq("is_delete","0");//未删除的

        //查询超时订单
        List<TbOrder> orders = tbOrderMapper.selectList(wrapper);
        for ( TbOrder tbOrder : orders){
            //记录订单变动日志
            TbOrderLog tbOrderLog = new TbOrderLog();
            IdWorker idWorker =new IdWorker();
            tbOrderLog.setId(String.valueOf(new IdWorker().nextId()));
            tbOrderLog.setOperateTime(new Date());
            tbOrderLog.setOrderStatus("4");
            tbOrderLog.setPayStatus(tbOrder.getPayStatus());
            tbOrderLog.setConsignStatus(tbOrder.getConsignStatus());
            tbOrderLog.setRemarks("超时订单,系统自动关闭");
            tbOrderLog.setOrderId(tbOrder.getId());
            //tbOrderLogMapper.insert(tbOrderLog);
            this.tbOrderLogMapper.insert(tbOrderLog);
            //更改订单状态
            tbOrder.setOrderStatus("4");
            tbOrder.setCloseTime(new Date());
            tbOrderMapper.updateById(tbOrder);
        }
    }

    //发货订单超时
    @Override
    public void takeOrderTimeOut() {
        //查询超时时间
        TbOrderConfig orderConfig =  tbOrderConfigMapper.selectById(1);
        Integer takeOrderTimeout = orderConfig.getTakeTimeout();//超时时间默认60分
        //LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(orderTimeout);
        DateTime dateTime = DateTime.now().minusDays(takeOrderTimeout);//得到超时的时间点
        //设置查询条件
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.le("create_time",dateTime);//创建时间小于超时时间
        wrapper.eq("order_status",2);//未收货的
        wrapper.eq("is_delete","0");//未删除的

        //查询超时订单
        List<TbOrder> orders = tbOrderMapper.selectList(wrapper);
        for ( TbOrder tbOrder : orders){
            //记录订单变动日志
            TbOrderLog tbOrderLog = new TbOrderLog();
            tbOrderLog.setId(String.valueOf(new IdWorker().nextId()));
            tbOrderLog.setOperater("system");
            tbOrderLog.setOperateTime(new Date());
            tbOrderLog.setOrderStatus("3");
            tbOrderLog.setPayStatus(tbOrder.getPayStatus());
            tbOrderLog.setConsignStatus(tbOrder.getConsignStatus());
            tbOrderLog.setRemarks("超时订单,订单自动完成");
            tbOrderLog.setOrderId(tbOrder.getId());
            tbOrderLogMapper.insert(tbOrderLog);
            //更改订单状态
            tbOrder.setOrderStatus("3");
            tbOrder.setCloseTime(new Date());
            tbOrderMapper.updateById(tbOrder);
        }
    }
    //订单完成超时不能售后
    @Override
    public void serviceOrderTimeOut() {
        //查询超时时间
        TbOrderConfig orderConfig =  tbOrderConfigMapper.selectById(1);
        Integer serviceOrderTimeout = orderConfig.getTakeTimeout();//超时时间默认60分
        //LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(orderTimeout);
        DateTime dateTime = DateTime.now().minusDays(serviceOrderTimeout);//得到超时的时间点
        //设置查询条件
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.le("create_time",dateTime);//创建时间小于超时时间
        wrapper.eq("order_status",3);//已完成的
        wrapper.eq("order_delete","0");//未删除的

        //查询超时订单
        List<TbOrder> orders = tbOrderMapper.selectList(wrapper);
        for ( TbOrder tbOrder : orders){
            //记录订单变动日志
            TbOrderLog tbOrderLog = new TbOrderLog();
            tbOrderLog.setId(String.valueOf(new IdWorker().nextId()));
            tbOrderLog.setOperater("system");
            tbOrderLog.setOperateTime(new Date());
            tbOrderLog.setOrderStatus("3");
            tbOrderLog.setPayStatus(tbOrder.getPayStatus());
            tbOrderLog.setConsignStatus(tbOrder.getConsignStatus());
            tbOrderLog.setRemarks("超时订单,不能售后");
            tbOrderLog.setOrderId(tbOrder.getId());
            tbOrderLogMapper.insert(tbOrderLog);
            //更改订单状态
            tbOrder.setCloseTime(new Date());
            tbOrderMapper.updateById(tbOrder);
        }
    }
    //订单完成超时不能五星好评
    @Override
    public void commentOrderTimeOut() {
        //查询超时时间
        TbOrderConfig orderConfig =  tbOrderConfigMapper.selectById(1);
        Integer commentOrderTimeout = orderConfig.getTakeTimeout();//超时时间默认60分
        //LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(orderTimeout);
        DateTime dateTime = DateTime.now().minusDays(commentOrderTimeout);//得到超时的时间点
        //设置查询条件
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.le("create_time",dateTime);//创建时间小于超时时间
        wrapper.eq("order_status",3);//已完成的
        wrapper.eq("is_delete","0");//未删除的

        //查询超时订单
        List<TbOrder> orders = tbOrderMapper.selectList(wrapper);
        for ( TbOrder tbOrder : orders){
            //记录订单变动日志
            TbOrderLog tbOrderLog = new TbOrderLog();
            tbOrderLog.setId(String.valueOf(new IdWorker().nextId()));
            tbOrderLog.setOperater("system");
            tbOrderLog.setOperateTime(new Date());
            tbOrderLog.setOrderStatus("3");
            tbOrderLog.setPayStatus(tbOrder.getPayStatus());
            tbOrderLog.setConsignStatus(tbOrder.getConsignStatus());
            tbOrderLog.setRemarks("超时订单，自动五星好评");
            tbOrderLog.setOrderId(tbOrder.getId());
            tbOrderLogMapper.insert(tbOrderLog);
            //更改订单状态
            tbOrder.setBuyerMessage("五星好评");
            tbOrder.setBuyerRate("是");
            tbOrder.setCloseTime(new Date());
            tbOrderMapper.updateById(tbOrder);
        }
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbOrderConfig> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbOrderConfig> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
        }
        return wrapper;
    }
}
